package com.lxmes.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.lxmes.business.domain.CcStockInventory;
import com.lxmes.business.service.ICcStockInventoryService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 库存盘点管理Controller
 *
 * @author lx
 * @date 2023-06-29
 */
@Api(tags = "库存盘点管理")
@RestController
@RequestMapping("/business/CcStockInventory")
public class CcStockInventoryController extends BaseController
{
    @Autowired
    private ICcStockInventoryService ccStockInventoryService;

    /**
     * 查询库存盘点管理列表
     */
    @ApiOperation("查询库存盘点管理列表")
    @PreAuthorize("@ss.hasPermi('business:CcStockInventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(CcStockInventory ccStockInventory)
    {
//        startPage();
        List<CcStockInventory> list = ccStockInventoryService.selectCcStockInventoryList(ccStockInventory);
        return getDataTable(list);
    }
    @ApiOperation("查询库存盘点管理列表")
    @PreAuthorize("@ss.hasPermi('business:CcStockInventory:list')")
    @GetMapping("/selectCcStockInventoryMain")
    public TableDataInfo selectCcStockInventoryMain(CcStockInventory ccStockInventory)
    {
        startPage();
        List<CcStockInventory> mainList = ccStockInventoryService.selectCcStockInventoryMain(ccStockInventory);
        return getDataTable(mainList);
    }

    /**
     * 导出库存盘点管理列表
     */
    @ApiOperation("导出库存盘点管理列表")
    @PreAuthorize("@ss.hasPermi('business:CcStockInventory:export')")
    @Log(title = "库存盘点管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CcStockInventory ccStockInventory)
    {
        List<CcStockInventory> list = ccStockInventoryService.selectCcStockInventoryList(ccStockInventory);
        ExcelUtil<CcStockInventory> util = new ExcelUtil<CcStockInventory>(CcStockInventory.class);
        util.exportExcel(response, list, "库存盘点管理数据");
    }

    /**
     * 获取库存盘点管理详细信息
     */
    @ApiOperation("获取库存盘点管理详细信息")
    @PreAuthorize("@ss.hasPermi('business:CcStockInventory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ccStockInventoryService.selectCcStockInventoryById(id));
    }

    @ApiOperation("获取库存盘点管理详细信息")
    @PreAuthorize("@ss.hasPermi('business:CcStockInventory:query')")
    @PostMapping(value = "/inventoryJs/{inventoryCode}")
    public AjaxResult inventoryJs(@PathVariable("inventoryCode") String inventoryCode)
    {
        return AjaxResult.success(ccStockInventoryService.inventoryJs(inventoryCode));
    }

    /**
     * 新增库存盘点管理
     */
    @ApiOperation("新增库存盘点管理")
    @PreAuthorize("@ss.hasPermi('business:CcStockInventory:add')")
    @Log(title = "库存盘点管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CcStockInventory ccStockInventory)
    {
        return toAjax(ccStockInventoryService.insertCcStockInventory(ccStockInventory));
    }
   /**
     * 库存盘点
     */
    @ApiOperation("库存盘点")
    @PreAuthorize("@ss.hasPermi('business:CcStockInventory:add')")
    @Log(title = "库存盘点", businessType = BusinessType.INSERT)
    @PostMapping("/handleInventory/{inventoryWay}")
    public AjaxResult handleInventory(@PathVariable String inventoryWay)
    {
        return toAjax(ccStockInventoryService.handleInventory(inventoryWay));
    }




    /**
     * 修改库存盘点管理
     */
    @ApiOperation("修改库存盘点管理")
    @PreAuthorize("@ss.hasPermi('business:CcStockInventory:edit')")
    @Log(title = "库存盘点管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CcStockInventory ccStockInventory)
    {
        CcStockInventory ccStockInventory1 = ccStockInventoryService.selectCcStockInventoryById(ccStockInventory.getId());
        if("0".equals(ccStockInventory1.getAppShow())){
            throw new RuntimeException("盘点表已核算完成,不能再修改！");
        }
        return toAjax(ccStockInventoryService.updateCcStockInventory(ccStockInventory));
    }

    /**
     * 删除库存盘点管理
     */
    @ApiOperation("删除库存盘点管理")
    @PreAuthorize("@ss.hasPermi('business:CcStockInventory:remove')")
    @Log(title = "库存盘点管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ccStockInventoryService.deleteCcStockInventoryByIds(ids));
    }
    /**
     * 导入库存盘点管理
     */
    @Log(title = "库存盘点管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CcStockInventory> util = new ExcelUtil<>(CcStockInventory.class);
        List<CcStockInventory> ccStockInventoryList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ccStockInventoryService.imporIteminfo(ccStockInventoryList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<CcStockInventory> util = new ExcelUtil<>(CcStockInventory.class);
        util.importTemplateExcel(response,"库存盘点管理");
    }




}
